/* eslint-disable */
import * as service from '../../services/collection/server';
import * as homeService from '../../services/home/homeService';
import * as managementLitigationService from '../../services/credit/managementLitigationService';
import config from '../../config/config';
import { message } from "antd";

export default {
  namespace: "collectionModel",
  state: {
    currentPage: 1,
    pageSize: config.PAGE_SIZE,
    dataSource: [],
    machineList: [],
    overdueTypeList: [],
    overdueReasonList: [],
    approvalList: [],
    loading: false,
    total: 0,
    searchData: {},
    personalizedColumn: [],
    allData: [],

    attachmentUploadModalVisible: false,
    attachmentUploadModalList: [],
    attachmentUploadModalLoading: false,
    masterDataCode: null,
    ownerShipInstitution: null,
    ownerShipInstitutionCode: null,
    basicId: null,
    menuData: [],

    treeColumns: [
      { title: '归属机构', key: 'affiliatedInstitutionName', },
      { title: '出库通知单号', key: 'outboundOrderNumber', },
      { title: '销售订单号', key: 'sourcePrimaryKey', },
      { title: '交易方名称', key: 'counterpartyName', },
      { title: '主数据编码', key: 'masterDataEncoding', },
      { title: '邓白氏编码', key: 'dunsCode', },
      { title: '催收等级', key: 'collectionLevelName', },
      { title: '催收金额', key: 'actuallyTake', },
      { title: '交易币种', key: 'currency', },
      { title: '付款条件', key: 'paymentTerm', },
      { title: '账期起算日', key: 'paymentDaysZeroDate', },
      { title: '到期日', key: 'maturityDate', },
      { title: '逾期天数', key: 'overDueDay', },
      { title: '业务员', key: 'businessPeopleName', },
      { title: '是否上传附件', key: 'isAttachment', },
      { title: '是否反馈', key: 'isFeedback', },
      { title: '最近反馈时间', key: 'feedbackTime', },
      { title: '催款跟进', key: 'followUpDesc', },
      { title: '是否已回款', key: 'isCancel' }
    ]
  },

  effects: {
    *init({ payload: params }, { select, call, put }) {
      let { pageUrl, userInfo } = yield select(state => state.globalModel);
      let { treeColumns } = yield select(state => state.collectionModel);
      if (pageUrl) {
        let data = treeColumns.map(item => item.key)
        yield put({
          type: "updateState", payload: { personalizedColumn: JSON.parse(JSON.stringify(data)), allData: JSON.parse(JSON.stringify(data)), currentPage: 1, pageSize: 10, }
        });
        yield put({ type: "functionalPermissionProcessing", payload: {} });
        yield put({ type: "customColumn", payload: { createId: JSON.parse(userInfo).id } });
      }
    },
    *getListDatas({ payload: { callback, ...params } }, { select, call, put }) {
      yield put({ type: "updateState", payload: { loading: true } });
      const res = yield call(service.getAllFollowUp, params);
      if (res.code == 200) {
        yield put({ type: "updateState", payload: { loading: false } });
        yield put({
          type: 'updateState',
          payload: { dataSource: res.data.records, total: res.data.total },
        });
        callback && callback instanceof Function && callback(res.data.records)
      } else {
        message.info("催款行动列表查询失败！");
      }
    },
    //自定义列
    *customColumn({ payload: params }, { select, put, call }) {
      //角色管理key:collection
      const res = yield call(homeService.getPersonalizedColumn, params);
      if (res.code == "200") {
        let list = res.data.filter(item => item.menuKey === "collection")
        let data = list.length > 0 && list[0].menuFields
        data ? yield put({ type: 'updateState', payload: { personalizedColumn: data.split(",") }, }) : null
      }
    },
    //附件查看
    *selectAttachment({ payload: params }, { select, call, put }) {
      yield put({ type: 'updateState', payload: { attachmentUploadModalLoading: true }, });
      const res = yield call(managementLitigationService.selectAttachment, params);
      if (res.code == "200") {
        yield put({ type: 'updateState', payload: { attachmentUploadModalList: res.data, attachmentUploadModalLoading: false }, });
      } else {
        message.info("附件查询失败！");
      }
    },
    //附件删除
    *deleteFileById({ payload: { params, data } }, { select, call, put }) {
      const res = yield call(managementLitigationService.deleteFileById, params);
      if (res.code == 200) {
        message.info("删除成功！");
        yield put({ type: 'selectAttachment', payload: data });
      } else {
        message.info("删除失败！");
      }
    },
    //附件下载
    *downloadFile({ payload: params }, { select, call, put }) {
      yield call(managementLitigationService.downloadFile, { id: params.id }, params.originalFileName);
    },
    //附件上传
    *uploadFile({ payload: { params, data } }, { select, call, put }) {
      const res = yield call(managementLitigationService.uploadFile, params);
      if (res.code == 200) {
        message.info("上传成功！");
      } else {
        message.info(res.message);
      }
      yield put({ type: 'selectAttachment', payload: data });
    },
    //功能权限处理
    *functionalPermissionProcessing({ payload: params }, { select, put, call }) {
      //角色管理key:collection
      let keyData = JSON.parse(sessionStorage.getItem("menuData")).list
      let key = keyData ? keyData.filter(item => item.key.indexOf("collection") !== -1).map(item => item.key) : []
      yield put({ type: "updateState", payload: { menuData: key } });
    },
  },

  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(location => {
        if (location.pathname == "/scmp/credit/collection") {
          dispatch({ type: 'init', payload: {} });
        }
      });
    },
  },

  reducers: {
    updateState(state, { payload }) {
      return { ...state, ...payload }
    },
  },
};
